import VueRouter from 'vue-router'
import Vue from 'vue'

// import Login from '../views/login/index.vue'

import auth from '@/utils/auth'

// import Layout from '../views/Layout.vue'

// import Home from '../views/home/index.vue'

// import Article from '../views/article/index.vue'

// import Publish from '../views/publish/index.vue'
Vue.use(VueRouter)

// const Login = () => import('@/views/login')
// const Layout = () => import('@/views/Layout')
// const Home = () => import('@/views/home')
// const Article = () => import('@/views/article')
// const Publish = () => import('@/views/publish')

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login')
    },
    {
      path: '/',
      component: () => import('@/views/Layout'),
      children: [
        {
          path: '/',
          component: () => import('@/views/home')
        },
        {
          path: '/article',
          component: () => import('@/views/article')
        },
        {
          path: '/publish',
          component: () => import('@/views/publish')

        }
      ]
    }
  ] // 路由规则

})

router.beforeEach((to, from, next) => {
  // 获取token
  const token = auth.getToken()
  // 不是访问登录，有没有token，跳转登录页面
  if (to.path !== '/login' && !token) return next('/login')
  // 其他情况放行
  next()
})

export default router
